package cn.openea.perpartner.space.web.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import cn.openea.perpartner.common.core.utils.ServletUtils;
import cn.openea.perpartner.space.web.entity.SpaceNotice;
import cn.openea.perpartner.space.web.mapper.SpaceNoticeMapper;
import cn.openea.perpartner.space.web.service.ISpaceNoticeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 基础公告(Notice)表服务实现类
 *
 * @author Weird077
 * @since 2023-03-23 14:27:22
 */
@Service
public class SpaceNoticeServiceImpl extends ServiceImpl<SpaceNoticeMapper, SpaceNotice> implements ISpaceNoticeService {

    @Override
    public SpaceNotice selectLatest() {
        LambdaQueryWrapper<SpaceNotice> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SpaceNotice::getStatus, 0);
        wrapper.orderByDesc(SpaceNotice::getCreateTime);
        wrapper.last("limit 1");
        return getOne(wrapper);
    }

    @Override
    public List<SpaceNotice> selectNoticeList(SpaceNotice notice) {
        LambdaQueryWrapper<SpaceNotice> wrapper = new LambdaQueryWrapper<>();
        UserAgent ua = UserAgentUtil.parse(ServletUtils.getRequest().getHeader("User-Agent"));
        if(ua.isMobile()){
            wrapper.eq(SpaceNotice::getStatus, 0);
        }
        if (StrUtil.isNotBlank(notice.getTitle())) {
            wrapper.like(SpaceNotice::getTitle, notice.getTitle());
        }
        wrapper.orderByDesc(SpaceNotice::getCreateTime);
        return list(wrapper);
    }
}

